set( QDPXX_INSTALL_DIR "@prefix@" )
set( QDPXX_PARALLEL_ARCH "@CONFIG_PARALLEL_ARCH@" )
set( QDPXX_INCLUDE_DIR "@prefix@/include" )
set( QDPXX_LIBDIR "@prefix@/lib")

# Get the Include directories out of the CFLAGS Line if they start with -I
string(STRIP "@HDF5_CXXFLAGS@ @LIBXML2_CXXFLAGS@ @QMP_CFLAGS@ @QMT_CXXFLAGS@" BASE_EXTRA_CXXFLAGS)
string(REPLACE " " ";" BASE_EXTRA_LIST ${BASE_EXTRA_CXXFLAGS})
foreach( ARG ${BASE_EXTRA_LIST})
        if( ${ARG} MATCHES "^-I")
		string(REPLACE "-I" "" STRIPPED_ARG ${ARG})
		list(APPEND EXTRA_INCLUDE_DIRS ${STRIPPED_ARG})
        endif()
endforeach()
# Find the library we built and add it as an imported target
# with appropriate include dir and location
function(add_qdpxx_lib LIBNAME)
 message(STATUS "Libname: ${LIBNAME}")

 # This will find the file, whether a .so or a .a appropriately
 find_library(LIB_${LIBNAME} NAMES ${LIBNAME} PATHS "${QDPXX_LIBDIR}")

 # Set up the library as an imported target
 add_library(${LIBNAME} UNKNOWN IMPORTED )

 # Explain where it is
 set_target_properties(${LIBNAME} PROPERTIES IMPORTED_LOCATION ${LIB_${LIBNAME}})
 # Set the include dir
 set_target_properties(${LIBNAME} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${QDPXX_INCLUDE_DIR}")
endfunction()

# Toplevel QDP++ library
add_qdpxx_lib(qdp)
# Add on Extra includes
foreach( INCDIR ${EXTRA_INCLUDE_DIRS})
 message(STATUS "Adding ${INCDIR}")
 set_target_properties(qdp PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${INCDIR})       
endforeach()



# XML Reader and writer
add_qdpxx_lib(XPathReader)
add_qdpxx_lib(xmlWriter)
target_link_libraries(qdp INTERFACE XPathReader xmlWriter)

# QIO and LIME
add_qdpxx_lib(lime)
add_qdpxx_lib(qio)
target_link_libraries(qio INTERFACE lime)
target_link_libraries(qdp INTERFACE qio)

# Check if using LibIntrin 
string(STRIP "@LIBINTRIN_DIR@" QDPXX_LIBINTRIN)
if( QDPXX_LIBINTRIN )
  add_qdpxx_lib(intrin)
  target_link_libraries(qdp INTERFACE intrin)
endif()

# Check if using FILEDB
string(STRIP "@FILEDB_DIR" QDPXX_FILEDB)
if( QDPXX_FILEDB ) 
   add_qdpxx_lib(filehash)
   add_qdpxx_lib(filedb)
   target_link_libraries(filedb INTERFACE filehash)
   target_link_libraries(qdp INTERFACE filedb)
endif()

# Add other LFlags and libs which we didn't build
string(STRIP "@LDFLAGS@ @HDF5_LDFLAGS@ @QMP_LDFLAGS@ @QMT_LDFLAGS@" QDPXX_LDFLAGS)
string(STRIP "@HDF5_LIBS@ @LIBXML2_LIBS@ @QMP_LIBS@ @LIBS@ @QMT_LIBS@" QDPXX_LIBS)

if( QDPXX_LDFLAGS ) 
  separate_argumnets(${QDPXX_LDFLAGS})
  target_link_options(qdp INTERFACE ${QDPXX_LDFLAGS})
endif()

if( QDPXX_LIBS )
  separate_arguments(${QDPXX_LIBS})
  target_link_libraries(qdp INTERFACE ${QDPXX_LIBS})
endif()

set( QDPXX_FOUND TRUE)

